package com.wolfpire.resource.controller.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.knowledge.dao.KnowledgeDao;
import com.wolfpire.knowledge.dao.SubjectKnowledgeRelationDao;
import com.wolfpire.knowledge.service.KnowledgeService;
import com.wolfpire.lessonplan.dao.LessonPlanDao;
import com.wolfpire.lessonplan.dao.SubjectLessonPlanRelationDao;
import com.wolfpire.lessonplan.service.LessonPlanService;
import com.wolfpire.profession.dao.ProfessionDao;
import com.wolfpire.resource.controller.dao.ResourceDao;
import com.wolfpire.resource.controller.service.ResourceService;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.subject.dao.SubjectDao;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.MD5;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.dao.AuthorityDao;
import com.wolfpire.system.dao.OrgDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.dao.RoleDao;
import com.wolfpire.system.dao.UserDao;
import com.wolfpire.system.dao.UserRoleRelationDao;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.Knowledge;
import com.wolfpire.system.model.LessonPlan;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.SubjectKnowledgeRelation;
import com.wolfpire.system.model.SubjectLessonPlanRelation;
import com.wolfpire.system.model.User;
import com.wolfpire.system.model.UserRoleRelation;
import com.wolfpire.system.service.AttachmentService;
import com.wolfpire.system.service.UserService;

@Service("resourceService")
public class ResourceServiceImpl extends BaseHibernateService<LessonPlan, Long> implements
	ResourceService {
	
	@Resource private SubjectDao subjectDao;
	@Resource private SubjectLessonPlanRelationDao subjectLessonPlanRelationDao;
	@Resource private AttachmentService attachmentService;
	
	@Resource private ResourceDao resourceDao;

	public List<LessonPlan> list(LessonPlan entity) {
		// TODO Auto-generated method stub
		return this.resourceDao.list(entity);
	}

	public LessonPlan getByTitle(String title) {
		// TODO Auto-generated method stub
		return this.resourceDao.getByTitle(title);
	}

	public Page<LessonPlan> setPageDataList(Page<LessonPlan> page,
			LessonPlan filterLessonPlan) {
		// TODO Auto-generated method stub
		return this.resourceDao.findLessonPlans(page, filterLessonPlan);
	}

	public List<LessonPlan> getLessonPlansBySubjectId(Long subjectId) {
		// TODO Auto-generated method stub
		return null;
	}

	public LessonPlan getCompleteLessonPlan(Long id) {
		LessonPlan lessonPlan = this.get(id);
		// 绑定专业
		SubjectLessonPlanRelation sk = this.subjectLessonPlanRelationDao.getSPRelation(id);
		lessonPlan.setSubjectId(sk.getSubjectId());
		lessonPlan.setKnowledgeId(sk.getKnowledgeId());
		Subject subject = this.subjectDao.get(sk.getSubjectId());
		/*if (null != subject) {
			knowledge.setName(subject.getName());
		}*/
		return lessonPlan;
	}

	@Override
	protected IBaseHibernateDao<LessonPlan, Long> getEntityDao() {
		// TODO Auto-generated method stub
		return this.resourceDao;
	}
	
	public void saveOrUpdate(LessonPlan lessonPlan) {
		if (null == lessonPlan.getId()) {
			this.resourceDao.saveOrUpdate(lessonPlan);
		} else {
			this.resourceDao.merge(lessonPlan);
		} 
		this.attachmentService.batchRelateAttachment(lessonPlan.getAttId(), lessonPlan.getId(), "lessonPlan");
		
		Long lessonPlanId = lessonPlan.getId();
		Long subjectId = lessonPlan.getSubjectId();
		Long knowledgeId = lessonPlan.getKnowledgeId();
		this.subjectLessonPlanRelationDao.relate(subjectId, lessonPlanId, knowledgeId);
	}

}
